﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;

public partial class Admin_UploadNeedTrainPersons : DLL.ShowLayer.BaseView
{
	private DLL.Training.Component clsTrainingComp;
	private DLL.TrainingPerson.Component clsTrainingPersonComp;
	private DLL.TrainingPerson.View clsTrainingPersonView;

	public Admin_UploadNeedTrainPersons()
		: base("idTotal")
	{
		

		AddTableColumn(new TonyNet4V1.Structs.TableShowColumn("培训班", "TrainingName"));
		AddTableColumn(new TonyNet4V1.Structs.TableShowColumn("唯一吗", "PersonID"));
		AddTableColumn(new TonyNet4V1.Structs.TableShowColumn("工号", "JobNumber"));
		AddTableColumn(new TonyNet4V1.Structs.TableShowColumn("姓名", "PersonName"));
		AddTableColumn(new TonyNet4V1.Structs.TableShowColumn("身份证", "IDCard"));

		lBtnEdit.Visible = false;
	}

	protected void Page_Load(object sender, EventArgs e)
	{
		//clsControls.ShowMySelfBuildTraining(ddlTraining, clsTrainingPersonView.nUserID);
		if (!IsPostBack)
		{
			clsControls.ShowMySelfBuildTraining(ddlTraining, clsTrainingPersonView.nUserID);
			if (ddlTraining.Items.Count == 0)
			{
				idTotal.Visible = false;
				idImport.Visible = false;
			}
		}
	}

	protected override void OnInit(EventArgs e)
	{
		clsTrainingComp = new DLL.Training.Component(Page);
		clsTrainingPersonComp = new DLL.TrainingPerson.Component(Page);
		clsTrainingPersonView = new DLL.TrainingPerson.View(Page);

		clsView = clsTrainingPersonView;

		base.OnInit(e);

		ddlTraining.AutoPostBack = true;

		BtnImport.Click += new EventHandler(BtnImport_Click);
		BtnStartApply.Click += new EventHandler(BtnStartApply_Click);
	}

	void BtnStartApply_Click(object sender, EventArgs e)
	{
		clsTrainingComp.StartApply(Convert.ToInt32(ddlTraining.Text));
		clsControls.ShowMySelfBuildTraining(ddlTraining, clsTrainingPersonView.nUserID);
	}

	void BtnImport_Click(object sender, EventArgs e)
	{
		string strFile = "";

		strFile = TonyNet4V1.CommonFunction.UploadFileToServer(fUpload, "~/ImportExcel/");

		if (strFile != "")
		{
			string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "Data Source=" + Server.MapPath(strFile) + ";" + "Extended Properties='Excel 12.0;HDR=Yes;IMEX=1';";

			OleDbConnection conn = new OleDbConnection(strConn);

			OleDbDataAdapter oada = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn);

			DataSet ds = new DataSet();

			oada.Fill(ds);

			DataTable dt = new DataTable();

			//oada.Fill(dt);

			dt = ds.Tables[0];

			//clsTrainingPersonComp.DeleteOneTrainingAllPerson(Convert.ToInt32(ddlTraining.Text));

			foreach (DataRow dr in dt.Rows)
			{
				DLL.TrainingPerson.Entity clsAddEntity = new DLL.TrainingPerson.Entity();

				clsAddEntity.TrainingID = Convert.ToInt32(ddlTraining.Text);
				//clsAddEntity.PersonID = dr[0].ToString();
				clsAddEntity.JobNumber = dr[0].ToString();
				clsAddEntity.PersonName = dr[1].ToString();
				clsAddEntity.IDCard = dr[2].ToString();

				clsTrainingPersonComp.Add(clsAddEntity);
			}

			//Response.Write("<script language=javascript>window.location.href=document.URL; </script>");
			TonyNet4V1.ShowMessageAlert.ShowMessage(Page, "导入完成！");
			BindGridView();
		}
		else
		{
			TonyNet4V1.ShowMessageAlert.ShowMessage(Page, "请选择参培人员名单文件！");
		}
	}

	protected override void BindGridViewNoQuery(GridView gv)
	{
		//base.BindGridViewNoQuery(gv);
		try
		{
			gv.DataSource = clsTrainingPersonComp.QueryByTrainingID(Convert.ToInt32(ddlTraining.Text));
			gv.DataKeyNames = new string[] { "ID" };
			gv.DataBind();
		}
		catch
		{
			TonyNet4V1.ShowMessageAlert.ShowMessage(Page, "没有需要添加参培人员培训班！");
		}
	}
}